<?php namespace Phpcmf\Controllers\Admin;

class Son extends \Phpcmf\Admin\Mform
{

    public function index() {

        $tid = dr_safe_filename($_GET['tid']);
        if (strlen($tid)) {
            $tid = intval($tid);
            $this->init['where_list'].= ' and tid='.$tid;
        }

        $this->init['order_by'] = 'displayorder ASC,inputtime desc'; //ASC倒排

        $p = [
            'cid' =>  $this->cid,
            'tid' =>  $tid,
        ];

        $this->_List($p);

        $stype = \Phpcmf\Service::M('content', 'quan')->son_type($this->index['son_type']);

        \Phpcmf\Service::V()->assign([
            'p' => $p,
            'stype' => $stype,
            'clink' => $this->_app_clink('mform'),
            'cbottom' => $this->_app_cbottom('mform'),
            'sname' => strlen($tid) ? $stype[$tid] : '全部',
            'menu' => \Phpcmf\Service::V()->get_value('menu').'<li> <a  href="javascript:dr_help(\'628\');"><i class="fa fa-question-circle"></i> '.dr_lang('在线帮助').'</a> <i class="fa fa-circle"></i> </li>',
        ]);
        \Phpcmf\Service::V()->display('son_list.html');
    }

    public function add() {
        $this->_Admin_Add();
    }

    public function edit() {
        $this->_Admin_Edit();
    }

    public function show_index() {
        $this->_Admin_Show();
    }

    public function order_edit() {
        $this->_Admin_Order();
    }

    public function del() {
        $this->_Admin_Del();
    }


    // 隐藏或者启用
    public function vip_edit() {

        $id = (int)\Phpcmf\Service::L('Input')->get('id');
        $row =\Phpcmf\Service::M()->table($this->init['table'])->get($id);
        !$row && $this->_json(0, dr_lang('数据不存在'));

        $v = $row['vip'] ? 0 : 1;
        \Phpcmf\Service::M()->table($this->init['table'])->update($id, ['vip' => $v]);

        exit($this->_json(1, dr_lang($v ? '已设置收费' : '已设置免费'), ['value' => $v]));
    }
	// 审核
	public function vip_shenhe() {
	
	    $id = (int)\Phpcmf\Service::L('Input')->get('id');
	    $row =\Phpcmf\Service::M()->table($this->init['table'])->get($id);
	    !$row && $this->_json(0, dr_lang('数据不存在'));
	
	    $v = $row['shenhe'] ? 0 : 1;
	    \Phpcmf\Service::M()->table($this->init['table'])->update($id, ['shenhe' => $v]);
	
	    exit($this->_json(1, dr_lang($v ? '已审核' : '取消审核'), ['value' => $v]));
	}
	// 首页推荐
	public function vip_tuijian() {
	
	    $id = (int)\Phpcmf\Service::L('Input')->get('id');
	    $row =\Phpcmf\Service::M()->table($this->init['table'])->get($id);
	    !$row && $this->_json(0, dr_lang('数据不存在'));
	
	    $v = $row['tuijian'] ? 0 : 1;
	    \Phpcmf\Service::M()->table($this->init['table'])->update($id, ['tuijian' => $v]);
	
	    exit($this->_json(1, dr_lang($v ? '已设置推荐' : '已经取消推荐'), ['value' => $v]));
	}
	// 圈子置顶
	public function vip_zhiding() {
	
	    $id = (int)\Phpcmf\Service::L('Input')->get('id');
	    $row =\Phpcmf\Service::M()->table($this->init['table'])->get($id);
	    !$row && $this->_json(0, dr_lang('数据不存在'));
	
	    $v = $row['zhiding'] ? 0 : 1;
	    \Phpcmf\Service::M()->table($this->init['table'])->update($id, ['zhiding' => $v]);
	
	    exit($this->_json(1, dr_lang($v ? '已设置置顶' : '已经取消置顶'), ['value' => $v]));
	}

    // 分类操作
    public function son_add() {

        if (IS_POST) {
            $name = dr_safe_replace($_POST['name']);
			$quanxian = dr_safe_replace($_POST['quanxian']);
			$yanse = dr_safe_replace($_POST['yanse']);
			$paixu = dr_safe_replace($_POST['paixu']);
            if (!$name) {
                $this->_json(0, dr_lang('名称没有填写'));
            }
			$name = [
				'id' => '',
				'name' => $name,
				'quanxian' => $quanxian,
				'yanse' => $yanse,
				'paixu' => $paixu,
			];
            $rt = \Phpcmf\Service::M('content', 'quan')->add_son_type($this->index, $name);
            if (!$rt['code']) {
                $this->_json(0, $rt['msg']);
            }
            $this->_json(1, dr_lang('操作成功'));
        }

        \Phpcmf\Service::V()->assign([
            'value' => '',
        ]);
        \Phpcmf\Service::V()->display('son_name.html');exit;
    }


    public function son_edit() {

        $tid = intval($_GET['tid']);
        $stype = \Phpcmf\Service::M('content', 'quan')->son_type($this->index['son_type']);

        if (IS_POST) {
            $name = dr_safe_replace($_POST['name']);
			$quanxian = dr_safe_replace($_POST['quanxian']);
			$yanse = dr_safe_replace($_POST['yanse']);
			$paixu = dr_safe_replace($_POST['paixu']);
            !$name && $this->_json(0, dr_lang('名称没有填写'));
			$stype[$tid] = [
				'id' => $tid,
				'name' => $name,
				'quanxian' => $quanxian,
				'yanse' => $yanse,
				'paixu' => $paixu,
			];
            //$stype[$tid] = $name;
            \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR))->update($this->cid, [
               'son_type' => dr_array2string($stype)
            ]);
            $this->_json(1, dr_lang('操作成功'));
        }

        \Phpcmf\Service::V()->assign([
            'value' => $stype[$tid],
        ]);
        \Phpcmf\Service::V()->display('son_name.html');exit;

    }

    public function son_del() {

        $tid = intval($_GET['tid']);
        $stype = \Phpcmf\Service::M('content', 'quan')->son_type($this->index['son_type']);
        !$stype[$tid] && $this->_json(0, dr_lang('分类不存在'));
        unset($stype[$tid]);
        \Phpcmf\Service::M()->table(dr_module_table_prefix(APP_DIR))->update($this->cid, [
            'son_type' => dr_array2string($stype)
        ]);

        $data = \Phpcmf\Service::M()->table($this->init['table'])->where('cid', $this->cid)->where('tid', $tid)->getAll();
        if ($data) {
            foreach ($data as $t) {
                $_POST['ids'][] = $t['id'];
            }
            $this->del();
        }

        $this->_json(1, dr_lang('操作成功'));

    }
}
